/**
 * @author Administrator
 */


// new_element=document.createElement("script");
// new_element.setAttribute("type","text/javascript");
// new_element.setAttribute("src","js/prototype-1.7.1.js");
// document.body.appendChild(new_element);

// 
// newscript=document.createElement('script');
// newscript.type='text/javascript';
// newscript.src='../js/prototype-1.7.1.js';
// head=document.getElementsByTagName('head')[0];
// head.appendChild(newscript)

document.write("<script language='javascript' src='../js/prototype-1.6.0.3.js'></script>");

//document.write("<script language='javascript' src='../js/prototype-1.7.1.js'></script>");

function prototypeTest(){

	document.writeln("Prototype库的版本为："+Prototype.Version+"<br/>")
	document.writeln("客户端浏览器是否为Firefox："+Prototype.Browser.Gecko+"<br/>")
	alert(Prototype.K('测试字符串'))
	
}

function clickHandler(){
	$("out").innerHTML="使用Prototype.js"
}

function clickHandler1(){
	$("out").innerHTML+=$("name1", "name2");
	//$("out").innerHTML+=$("name1").value;
}

function IDTest(){
	var eleList=$$("span");
	for(var i=0; i<eleList.length; i++){
		document.writeln('$$("#a")的'+i+'个元素为：'+eleList[i].innerHTML+"<br/>");
	}
	var eleList=$$("#a");
	for(var i=0; i<eleList.length; i++){
		document.writeln('$$("#a")的'+i+'个元素为：'+eleList[i].innerHTML+"<br/>");
	}
	var eleList=$$(".pt9");
	for(var i=0; i<eleList.length; i++){
		document.writeln('$$(".pt9")的'+i+'个元素为：'+eleList[i].innerHTML+"<br/>");
	}
}

function file2Array(){
	var fileList=$("menuBar").getElementsByTagName("div");
	document.writeln(fileList+"<br />");
	for(obj in fileList){
		document.writeln(fileList[obj]+"<br/>");
	}
	document.writeln("<hr/>");
	var fileArray=$A(fileList);
	for(var i=0; i<fileArray.length; i++){
		document.writeln(fileArray[i].innerHTML+"<br/>");
	}
}

function Str2Array(){
	var str="crazyit";
	var strArray=$A(str);
	for(var i=0; i<strArray.length; i++){
		document.writeln(strArray[i]+"<br/>");
	}
}

function showForm(){
	$("show").innerHTML=$F("text1")+"<br/>"+$F("text2")+"<br/>"+$F("text3");
}

function newHash(){
	var nav=$H(navigator);
	alert(nav.inspect());
	nav=$H({});
	alert(nav.inspect());
}

function objRange(){
	var range=$R(10, 20, false);
	range.each(function(value, index){
		document.writeln(value+"<br/>");
	})
}

function aa(){
	return "hello";
}

function tryTheseTest(){
	var b=Try.these(aa);
	alert(b);
}

function jsonTest(){
	var data = {name: 'Violet', occupation: 'character', age: 25 };
	document.writeln(Object.toJSON(data));
	document.writeln("<br/>");
	var hash=$H({name: 'Violet', occupation: 'character', age: 25 });
	//document.writeln(hash.toJSON());//只显示OBJECT  http://prototypejs.org/download
	document.writeln(hash.toJSON());
}

function toPJson(){
	var Person = Class.create();
	Person.prototype = {
  		initialize: function(name, age) {
    		this.name = name;
    		this.age = age;
  		}, 
  		 
  		toJSON: function() {
    		return ('My name is ' + this.name + 
      			' and I am ' + this.age + ' years old.').toJSON();
  		}
	};
	var john = new Person('John', 49);
	document.writeln(Object.toJSON(john));
}

function testInputJson(){
	
	Element.addMethods('input', {
  		toJSON: function(element) {
    		element = $(element);
    		return element.name.toJSON() + ": " + element.getValue().toJSON();
  		}
	})

	document.writeln("<br/>"+Object.toJSON($('firstname')));
}

function alertJSON(){
	var data = '{ "name": "Violet", "occupation": "character" }'.evalJSON();
	alert(data.name);
}

function chg(){
	Element.addClassName("up", "solid");
}

function chg1(){
	Element.toggle("div1");
	Element.toggle("div2");
	Element.toggle("div3");
}

function eachCollection(){
	var a=[20, 30, 40, 50];
	a.each(function(value, index){
		$("show").innerHTML += "第"+index+"元素的值是："+value+"<br/>";
	});
}

function rangeObjectTest(){
	
	var range=new ObjectRange(2, 9, true);
	range.each(function(ele, index){
		document.writeln("索引"+index+"处的值为："+ele+"<br/>");	
	});
	
	var ra=$R('a', 'e');
	document.writeln($A(ra));
	
	alert(ra.include('b'));
}

function hashTest(){
	var person=
	{
		name:'yeeku',
		age:29
	};
	var h=$H(person);
	document.writeln(h.toQueryString()+"<br/>");
	document.writeln(h.get('age'));
	var person=
	{
		name:'yeeku',
		age:29
	};
	var teacher=
	{
		name:'yekku.H.lee',
		gender:'male'
	};
	var h=$H(person).merge($H(teacher));
	document.writeln(h.toQueryString()+"<br/>");
	
	h.update({age:30, subject:"java"});
	document.writeln(h.toQueryString()+"<br/>");
	
	h.each(function(pair){
		document.writeln(pair.key+"-->"+pair.value+"<br/>");
	});
}

function testEvent(){
	Event.observe("ok", "click", function(event){
		alert("是否为左键事件："+event.isLeftClick());
		alert("事件源："+event.element().value);
		alert("最近的td元素："+event.findElement("td").innerHTML);
	});
}

function templateTest(){
	objArr=[
		{book:'Struts2 权威指南', author:"李刚"},
		{book:'疯狂Java讲义', author:"李刚"},
		{book:'轻量级Java EE企业应用实战', author:"李刚"}
	]
	var template=new Template("书名是#{book}, 作者是#{author}.");
	for(var i=0; i<objArr.length; i++){
		document.writeln(template.evaluate(objArr[i]));
	}
}

function testClass(){
	var Person=Class.create({
		initialize:function(name, age){
			this.name=name;
			this.age=age;
		},
		info:function(){
			alert(this.name+"的年龄是："+this.age);
		}
	});
	var p=new Person('yeeku', 30);
	p.info();
	var Student = Class.create(Person , 
	{
		//定义新的构造器，$super形参代表父类同名方法
		initialize: function($super , name , age , grade)
		{
			$super(name, age);
			this.grade = grade;
		},
		//定义一个普通方法
		study: function()
		{
			//调用从Person继承到的info()方法
			this.info();
			alert("我上 " + this.grade + " 年级");
		}
	});
	//创建Student对象
	var s = new Student('wawa' , 8 , 3);
	s.study();
}

function FormObserver(){
	new Form.Observer("test", 1, function(){
		alert(this.getValue());
	});
}

function FormElementObserver(){
	new Form.Element.Observer("user", 1, function(){
		alert(this.getValue())
	});
}

function arrayTest(){
	var a=['疯狂Java讲义', '轻量级Java EE企业应用实战'];
	var b=['wawa', 'anno'];
	var c=[a, b];
	c.each(function(ele, index){
		document.writeln('第'+index+'个元素时：'+ele+'<br/>');
	});
	
	var d=[c, 'china'];
	document.writeln('d数组长度是：'+d.length+'<br/>');
	var e=d.flatten();
	document.writeln(e.size()+e);
}

function stringTest(){
	//用于示范删除XML和HTML标记
    var str1 ='<a>dfd</a>';
    //输出结果为dfd
   alert("'<a>dfd</a>'.stripTags()的结果为："
		+ str1.stripTags());
    //用于示范删除脚本
    var str2 ='<script>dfd<\/script>';
    //删除脚本后字符串为空字符串
    alert("'<script>dfd<\/script>'.stripScripts()的结果为："
		+ str2.stripScripts());
    //示范将HTML标记转义
    var str3 ='<a>dfd</a>';
    //输出结果为&lt;a&gt;dfd&lt;/a&gt;
    alert("'<a>dfd</a>'.escapeHTML()的结果为："
		+ str3.escapeHTML());
    //示范反转义HTML标记
    var str4 ='&lt;a&gt;dfd&lt;/a&gt;';
    //输出结果为<a>dfd</a>
    alert("'&lt;a&gt;dfd&lt;/a&gt;'.unescapeHTML()的结果为："
		+ str4.unescapeHTML());
	//示范执行字符串中的脚本
    var str5 ='<script>alert("====");<\/script>';
    //弹出警告对话框
    str5.evalScripts();
    //示范取得字符串中的脚本
    var str6 ='<script>alert("====");<\/script>'
		+ '<script>alert("xxxx");<\/script>';
    //输出一个长度为2的字符串数组。
    alert(str6.extractScripts());
    var str7 = 'abc-xyz-ghi';
    var str8 = str7.camelize();
	alert("'abc-xyz-ghi'.camelize()的值为：" + str8);
	var str9 = str8.underscore().dasherize();
	//再次恢复'abc-xyz-ghi';
	alert(str9);
	var book = '轻量级Java EE企业应用实战';
	alert("'轻量级Java EE企业应用实战'.truncate(8 , '...')的值："
		+ book.truncate(10 , '...'));
	var str10 = 'java struts hibernate';
	//用逗号（,）替换空白，将输出java,struts,hibernate
	alert(str10.gsub('\\s+' , ','));
	//使用自定义替换，将所有单词首字母大写，并在单词后添加逗号（,）
	//将输出Java, Struts, Hibernate
	alert(str10.gsub('\\w+' , function(match)
	{
		return match[0].capitalize() + ",";
	}));
	//将依次输出java,struts,hibernate
	str10.scan("\\w+" , function(match)
	{
		alert(match);
	});
}

function bindFunctionTest(){
	var person ={
		name : 'yeeku',
		info : function(){
			alert(this.name);
		}	
		
	};
	person.info();
	
	function runFn(f){
		f();
	}
	
	window.name='疯狂Java讲义';
	
	runFn(person.info);
	
	runFn(person.info.bind(person));
	
	function test(name, age){
		alert("此人名为："+name+"\n"+"年龄为："+age);
	}
	
	var newTest=test.curry('yeeku');
	newTest(31);
	
	var fn1=function(target, name){
		target.name=name;
	}
	
	var fn2=fn1.methodize();
	fn2("疯狂XML讲义");
	alert(name);
}
